<?php

require_once APPPATH . 'libraries/VideoEntry.php';

/**
 * Абстрактный предок парсеров кинопартнерок
 *
 * @author Gennadiy Kozlenko
 */
abstract class PartnerParser_Abstract {
    
    /**
     * CI instance
     * @var object
     */
    protected $obj = null;
    
    /**
     * XML Link
     * @var string
     */
    protected $xmlLink = '';
    
    /**
     * Partner ID
     * @var int
     */
    protected $idPartner = 0;
    
    /**
     * Timeout
     * @var int
     */
    protected $timeout = 600;
    
    /**
     * Class constructor
     */
    public function __construct() {
        $this->obj =& get_instance();
        $this->obj->load->helper('http');
    }
    
    /**
     * Parse partners link
     */
    abstract public function parse();

    /**
     * Set XML Link
     * @param string $link
     */
    public function setXmlLink($link) {
        $this->xmlLink = $link;
    }
    
    /**
     * Get XML Link
     */
    public function getXmlLink() {
        return $this->xmlLink;
    }
    
    /**
     * Set Partner ID
     * @param int $idPartner
     */
    public function setIdPartner($idPartner) {
        $this->idPartner = $idPartner;
    }
    
    /**
     * Get Partner ID
     */
    public function getIdPartner() {
        return $this->idPartner;
    }
    
    /**
     * Set timeout
     * @param int $timeout
     */
    public function setTimeout($timeout) {
        $this->timeout = $timeout;
    }
    
    /**
     * Get timeout
     */
    public function getTimeout() {
        return $this->timeout;
    }

    /**
     * Search videos
     * @param VideoEntry $entry
     */
    protected function searchVideos(VideoEntry $entry) {
        $videos = array();
        $titles = explode(' / ', $entry->getTitle());
        array_push($titles, $entry->getTitle());
        $titles = array_unique($titles);
        $origTitles = explode(' / ', $entry->getOrigTitle());
        array_push($origTitles, $entry->getOrigTitle());
        $origTitles = array_unique($origTitles);
        $ent = new VideoEntry();
        $hashes = array();
        foreach ($titles as $title) {
            $ent->setTitle($title);
            array_push($hashes, $ent->getTitleHash());
            foreach ($origTitles as $origTitle) {
                $ent->setOrigTitle($origTitle);
                array_push($hashes, $ent->getTitleHash());
            }
        }
        $this->obj->db->select('id_video')
            ->from('videos')
            ->where_in('titlehash', $hashes)
            ->where('year', $entry->getYear());
        $query = $this->obj->db->get();
        if (0 < $query->num_rows()) {
            foreach ($query->result_array() as $row) {
                array_push($videos, $row['id_video']);
            }
        }
        return $videos;
    }
    
    /**
     * Register partner code for video
     * @param int $idVideo
     * @param int $idPartner
     * @param string $externalId
     */
    protected function registerPartner($idVideo, $idPartner, $externalId) {
        $this->obj->db->select('id_video')
            ->from('video_partners')
            ->where('id_video', $idVideo)
            ->where('id_partner', $idPartner);
        $query = $this->obj->db->get();
        if (0 == $query->num_rows()) {
            $this->obj->db->insert('video_partners', array(
                'id_video' => $idVideo,
                'id_partner' => $idPartner,
                'external_id' => $externalId
            ));
        }
    }
    
    /**
     * Get contnet
     * @param string $url
     */
    protected function getContent($url) {
        return get_content($url, array(), $this->timeout);
    }

}
